<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../javascript/jquery.min.js"></script>

</head>
<script>
    $(document).ready(function () {
        
    });
</script>

<body>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>添加教室</legend>
    </fieldset>
    <form class="layui-form layui-form-pane" action="">

        <div class="layui-form-item">
            <label class="layui-form-label">教室名</label>
            <div class="layui-input-inline">
                <input type="text" name="roomName" id="roomName" autocomplete="off" lay-verify="title" placeholder="请输入教室名字" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">搜索选择框</label>
            <div class="layui-input-inline">
                <select name="modules" id="roomLayer" lay-verify="required" lay-search="">
                    <option value="1">第一层</option>
                    <option value="2">第二层</option>
                    <option value="3">第三层</option>
                    <option value="4">第四层</option>
                    <option value="5">第五层</option>
                    <option value="6">第六层</option>
                    <option value="7">第七层</option>
                </select>
            </div>
        </div>
       
        <div class="layui-form-item">
            <label class="layui-form-label">教室空间</label>
            <div class="layui-input-inline">
                <input type="text" name="roomSpace" id="roomSpace" autocomplete="off" lay-verify="num" placeholder="请输入整数教室空间"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <input type="text" name="mark" id="mark" autocomplete="off" placeholder="请输入备注" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-mid">
            <div class="layui-form-item">

            </div>
            <button class="layui-btn" lay-submit="" style="position:absolute;left:140px;"
                lay-filter="demo">确定</button>
        </div>
        
    </form>

    <script>
         layui.use(['form', 'layedit', 'laydate'], function () {
            let tag = window.location.href.split("=");
            if (tag.length == 3 && tag[1] == "update") {
                let id = window.location.href.split("=")[2];
                var u = "http://127.0.0.1:8088/classroom/find?id=" + id;
                $.get(u, function (data, status) {
                    console.log(u)
                    var ret = JSON.parse(JSON.stringify(data));
                    if (ret.status == 1) {
                        alert(ret.msg);
                    } else {
                        $("#roomName").val(ret.data.roomName)
                        $("#roomSpace").val(ret.data.roomSpace)
                        jQuery("#roomLayer  option:selected").text(ret.data.roomLayer)
                        $("#mark").val(ret.data.mark);
                    }
                });
            }
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate;

            //创建一个编辑器
            var editIndex = layedit.build('LAY_demo_editor');

            //自定义验证规则
            var patrn = /^\+?[1-9][0-9]*$/;
            form.verify({
                title: function (value) {
                    if (value = null || value.length == 0) {
                        return '教室名不能为空';
                    }
                }
                , num: function (value) {
                    if (value = null || value.length == 0||patrn.exec(value) == null) {
                        return '教室空间只能为非空数字';
                    }
                }
                , content: function (value) {
                    layedit.sync(editIndex);
                }
            });
            //监听提交
            form.on('submit(demo)', function (data) {
                var url;
                //Integer id, String roomName, Integer roomSpace, Integer roomLaye
                if (tag.length == 2) {
                    url = "http://127.0.0.1:8088/classroom/add?roomName=" + $("#roomName").val() + "&roomSpace=" + $("#roomSpace").val() + "&roomLayer=" + $("#roomLayer  option:selected").val()+"&mark="+$("#mark").val();
                } else if (tag.length == 3) {
                    let id = window.location.href.split("=")[2];
                    url = "http://127.0.0.1:8088/classroom/update?id=" + id + "&roomName=" + $("#roomName").val() + "&roomSpace=" + $("#roomSpace").val() + "&roomLayer=" + $("#roomLayer  option:selected").val()+"&mark="+$("#mark").val();
                }
                console.log(url)
                var datas = JSON.parse(JSON.stringify(data.field));
                $.get(url, function (data, status) {
                    var ret = JSON.parse(JSON.stringify(data));
                    if (ret.status == 1) {
                        layer.alert(ret.msg);
                    } else {
                        layer.alert(ret.msg);
                        window.parent.location.reload();//刷新父页面
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭当前iframe页面
                        parent.location.reload();//刷新父页面
                    }

                });
                return false;
            });
        });
    </script>

</body>

</html>